/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import dao.CustomerFacade;
import dao.SoftwareFacade;
import entity.Customer;
import entity.Software;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import util.UAgentInfo;

/**
 *
 * @author HumanBooster
 */
@ManagedBean(eager = true)
@SessionScoped
public class MobileController implements Serializable {

    @EJB
    private CustomerFacade customerFacade;
    private String login = "Tom";
    private String password = "pwd";
    private Customer account = new Customer();

    public Customer getAccount() {
        return account;
    }

    public void setAccount(Customer account) {
        this.account = account;
    }
    
    
    @EJB
    private SoftwareFacade softwareFacade;
    private List<Software> softwares = new ArrayList<Software>();
    
    @PostConstruct
    public List<Software> getSoftwares() {
        softwares = softwareFacade.findAll();
        System.out.println("bean created this = " + softwares.size());
        return softwares;
    }
    
    public void setSoftwares(List<Software> softwares) {
        this.softwares = softwares;
    }
    
    public String getLogin() {
        return login;
    }
    
    public void setLogin(String login) {
        this.login = login;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String doShowLogin() {
        return "login.xhtml";
    }
    
    public String doShowIndex() {
        return "index.xhtml";
    }
    
    public String doShowProducts() {
        return "products.xhtml";
    }
    
    public String doShowCreateAccount() {
        return "createAccount.xhtml";
    }
    
    public String doCreateAccount() {
        account.setRole("user");
        account.setRegisterDate(new Date());
        customerFacade.create(account);
        return "index.xhtml";
    }
    
    public String doLogin() {
        
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        UAgentInfo ua = new UAgentInfo(request);
        
        
        
        System.out.println("smartphone = " + ua.detectSmartphone());
        return "index.xhtml";
    }

    /**
     * Creates a new instance of MobileController
     */
    public MobileController() {
    }
}
